{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Corrects mis-attributed RGI sub-region attributes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some glaciers (140) have been attributed to the wrong RGI sub-region number. The majority (116) are nominal glacier at high lats, but some other are simply close to the border and were obviously mis-attributed.\n",
    "\n",
    "This script corrects the shapefiles' attributes and writes the files in a new directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = 'RGI62'\n",
    "in_dir = 'RGI62_update_logs'\n",
    "rgi_version = '61'\n",
    "replace_str = lambda x : x.replace('rgi61', 'rgi62')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import geopandas as gpd\n",
    "from oggm import utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "in_dir = os.path.abspath(in_dir)\n",
    "out_dir = os.path.abspath(out_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RGIId</th>\n",
       "      <th>O1Region</th>\n",
       "      <th>O2Region</th>\n",
       "      <th>NEW_REG</th>\n",
       "      <th>NEW_SREG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>RGI60-01.12756</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>None</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>RGI60-01.15201</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>None</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>RGI60-01.15481</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>None</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>RGI60-01.15482</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>None</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>RGI60-01.15485</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>None</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            RGIId O1Region O2Region NEW_REG NEW_SREG\n",
       "0  RGI60-01.12756        1        5    None        6\n",
       "1  RGI60-01.15201        1        2    None        5\n",
       "2  RGI60-01.15481        1        2    None        5\n",
       "3  RGI60-01.15482        1        2    None        5\n",
       "4  RGI60-01.15485        1        2    None        5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The wrong region\n",
    "reg_cor = gpd.read_file(os.path.join(in_dir, 'entities_misplaced', 'subreg_misplaced_after_cor.shp'))\n",
    "reg_cor = reg_cor[['RGIId', 'O1Region', 'O2Region', 'NEW_REG', 'NEW_SREG']].copy()\n",
    "reg_cor['NEW_SREG'] = [str(int(s.split('-')[1])) for s in reg_cor['NEW_SREG']]\n",
    "reg_cor.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Loop over RGI files\n",
    "out = []\n",
    "for reg in range(1, 20):\n",
    "    # Read in\n",
    "    f = utils.get_rgi_region_file('{:02d}'.format(reg), version=rgi_version)\n",
    "    sh = gpd.read_file(f)\n",
    "    \n",
    "    # Replace\n",
    "    _reg_cor = reg_cor.loc[reg_cor.O1Region == sh.iloc[0].O1Region]\n",
    "    ides = sh.RGIId.isin(_reg_cor.RGIId)\n",
    "    if ides.sum() > 0:\n",
    "        sh.loc[ides, 'O2Region'] = _reg_cor.NEW_SREG.values\n",
    "        \n",
    "    # Write out\n",
    "    of = os.path.join(out_dir, os.path.basename(f).replace('.shp', ''))\n",
    "    utils.mkdir(replace_str(of))\n",
    "    of = os.path.join(of, os.path.basename(f))\n",
    "    sh.to_file(replace_str(of))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
